package com.learn.security.modules.wx.mp.handler;

import com.learn.security.common.constant.ImgUrlConstants;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class ScanHandler extends AbstractHandler {

    /**
     * 车妈妈
     */
    private static final String CARMUM = "carmum";

    @Value("${server.domain}")
    private String domain;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        logger.info("扫码二维码");
        if (wxMpXmlMessage.getEventKey().contains(CARMUM)){
            // 如果是车妈妈二维码订阅,则推送图文消息
            WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
            item.setTitle("您的礼品到啦!!!");
            item.setDescription("点击领取赠送礼品!");
            item.setPicUrl(ImgUrlConstants.E_JIA_BAO_LOGO);
            item.setUrl(domain + "spring-security-learn/index.html");
            return WxMpXmlOutMessage.NEWS()
                    .addArticle(item)
                    .fromUser(wxMpXmlMessage.getToUser())
                    .toUser(wxMpXmlMessage.getFromUser())
                    .build();
        }
        // 扫码事件处理
        return null;
    }
}
